home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / music / cthugha5.zip / CTHU5SRC.ZIP / GETOPT.C < prev    next >
C/C++ Source or Header  |  1994-08-19  |  4KB  |  136 lines

  1. //
  2. // Cthugha - Audio Seeded Image Processing
  3. //
  4. // Zaph, Digital Aasvogel Group, Torps Productions 1993-1994
  5. //
  6.  
  7. /*
  8.     Copyright (c) 1986,1991 by Borland International Inc.
  9.     All Rights Reserved.
  10. */
  11.  
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <stdio.h>
  16.  
  17. int    optind    = 1;    /* index of which argument is next    */
  18. char   *optarg;        /* pointer to argument of current option */
  19. int    opterr    = 1;    /* allow error message    */
  20.  
  21. static    char   *letP = NULL;    /* remember next option char's location */
  22. static    char    SW = '-';         /* DOS switch character, either '-' or '/' */
  23.  
  24. /*
  25.   Parse the command line options, System V style.
  26.  
  27.   Standard option syntax is:
  28.  
  29.     option ::= SW [optLetter]* [argLetter space* argument]
  30.  
  31.   where
  32.     - SW is either '/' or '-', according to the current setting
  33.       of the MSDOS switchar (int 21h function 37h).
  34.     - there is no space before any optLetter or argLetter.
  35.     - opt/arg letters are alphabetic, not punctuation characters.
  36.     - optLetters, if present, must be matched in optionS.
  37.     - argLetters, if present, are found in optionS followed by ':'.
  38.     - argument is any white-space delimited string.  Note that it
  39.       can include the SW character.
  40.     - upper and lower case letters are distinct.
  41.  
  42.   There may be multiple option clusters on a command line, each
  43.   beginning with a SW, but all must appear before any non-option
  44.   arguments (arguments not introduced by SW).  Opt/arg letters may
  45.   be repeated: it is up to the caller to decide if that is an error.
  46.  
  47.   The character SW appearing alone as the last argument is an error.
  48.   The lead-in sequence SWSW ("--" or "//") causes itself and all the
  49.   rest of the line to be ignored (allowing non-options which begin
  50.   with the switch char).
  51.  
  52.   The string *optionS allows valid opt/arg letters to be recognized.
  53.   argLetters are followed with ':'.  Getopt () returns the value of
  54.   the option character found, or EOF if no more options are in the
  55.   command line.     If option is an argLetter then the global optarg is
  56.   set to point to the argument string (having skipped any white-space).
  57.  
  58.   The global optind is initially 1 and is always left as the index
  59.   of the next argument of argv[] which getopt has not taken.  Note
  60.   that if "--" or "//" are used then optind is stepped to the next
  61.   argument before getopt() returns EOF.
  62.  
  63.   If an error occurs, that is an SW char precedes an unknown letter,
  64.   then getopt() will return a '?' character and normally prints an
  65.   error message via perror().  If the global variable opterr is set
  66.   to false (zero) before calling getopt() then the error message is
  67.   not printed.
  68.  
  69.   For example, if the MSDOS switch char is '/' (the MSDOS norm) and
  70.  
  71.     *optionS == "A:F:PuU:wXZ:"
  72.  
  73.   then 'P', 'u', 'w', and 'X' are option letters and 'F', 'U', 'Z'
  74.   are followed by arguments.  A valid command line may be:
  75.  
  76.     aCommand  /uPFPi /X /A L someFile
  77.  
  78.   where:
  79.     - 'u' and 'P' will be returned as isolated option letters.
  80.     - 'F' will return with "Pi" as its argument string.
  81.     - 'X' is an isolated option.
  82.     - 'A' will return with "L" as its argument.
  83.     - "someFile" is not an option, and terminates getOpt.  The
  84.       caller may collect remaining arguments using argv pointers.
  85. */
  86.  
  87. int    getopt(int argc, char *argv[], char *optionS)
  88. {
  89.     unsigned char ch;
  90.     char *optP;
  91.  
  92.     if (argc > optind) {
  93.         if (letP == NULL) {
  94.             if ((letP = argv[optind]) == NULL || (*(letP++) != '-'))  goto gopEOF;
  95.  
  96.             if (*letP == '-') {
  97.                 optind++;  goto gopEOF;
  98.             }
  99.             if (*letP == '/') {
  100.                 optind++;  goto gopEOF;
  101.             }
  102.         }
  103.         if (0 == (ch = *(letP++))) {
  104.             optind++;  goto gopEOF;
  105.         }
  106.         if (':' == ch  ||  (optP = strchr(optionS, ch)) == NULL)  
  107.             goto gopError;
  108.         if (':' == *(++optP)) {
  109.             optind++;
  110.             if (0 == *letP) {
  111.                 if (argc <= optind)  goto  gopError;
  112.                 letP = argv[optind++];
  113.             }
  114.             optarg = letP;
  115.             letP = NULL;
  116.         } else {
  117.             if (0 == *letP) {
  118.                 optind++;
  119.                 letP = NULL;
  120.             }
  121.             optarg = NULL;
  122.         }
  123.         return ch;
  124.     }
  125. gopEOF:
  126.     optarg = letP = NULL;  
  127.     return EOF;
  128.  
  129. gopError:
  130.     optarg = NULL;
  131.     errno  = EINVAL;
  132.     if (opterr)
  133.         perror ("get command line option");
  134.     return ('?');
  135. }
  136.